package com.zhiming.item.cotroller;


import com.zhiming.common.pojo.PageResult;
import com.zhiming.item.pojo.Article;
import com.zhiming.item.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 文章 Controller
 */

@RestController
@Slf4j
public class ArticleController {
    @Autowired
    private ArticleService articleService;



    /**
     * 根据id查询文章
     */
    @GetMapping("article/{id}")
    public ResponseEntity<Article> findById(@PathVariable("id") Integer id){
        log.info("【根据id查询文章】id="+id);
        Article article = articleService.findById(id);
        return ResponseEntity.ok(article);
    }


    /**
     * 动态分页查询文章
     */
    @GetMapping("/article/page")
    public ResponseEntity<PageResult<Article>> articlePageQuery(
            @RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            @RequestParam(value = "menu_id",required = false) Integer menu_id,
            @RequestParam(value = "title",required = false) String title
    ){
        log.info("【动态分页查询文章,查询第"+currentPage+"页,每页显示"+pageSize+"条文章,过滤导航菜单menu_id="+menu_id+",过滤文章标题title="+title+"】");
        PageResult<Article> pageResult = articleService.articlePageQuery(currentPage,pageSize,menu_id,title);
        return ResponseEntity.ok(pageResult);
    }


    @GetMapping("/article/newest")
    public ResponseEntity<List<Article>> getNewestArticle(){
        log.info("查询最新的10篇文章");
        List<Article> newestArticleList = articleService.getNewestArticle();
        return ResponseEntity.ok(newestArticleList);
    }


}
